package com.ruoyi.serviceItem.service.impl;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.serviceItem.domain.IncomeExpenseRecord;
import com.ruoyi.serviceItem.domain.Order;
import com.ruoyi.serviceItem.domain.VerificationRecord;
import com.ruoyi.serviceItem.mapper.IncomeExpenseRecordMapper;
import com.ruoyi.serviceItem.mapper.OrderMapper;
import com.ruoyi.serviceItem.mapper.VerificationRecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class OrderServiceImpl {
    private final OrderMapper orderMapper;
    private final VerificationRecordMapper verificationRecordMapper;
    private final IncomeExpenseRecordMapper incomeExpenseRecordMapper;


//    @Transactional(rollbackFor = Exception.class)
    public Date verifyOrder(Long orderId) {
        // 1. 验证订单状态
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order == null) {
            throw new RuntimeException("订单不存在");
        }
        if (order.getExt2() != null && order.getExt2() == 1L)  {
            throw new RuntimeException("订单已核销");
        }

        // 2. 更新订单核销状态
        order.setExt2(1L);
        orderMapper.updateByPrimaryKeySelective(order);

        // 3. 创建核销记录
        Date verificationTime = new Date();
        VerificationRecord verificationRecord = new VerificationRecord();
        verificationRecord.setOrderId(orderId);
        verificationRecord.setVerificationCode(order.getVerificationCode());
        verificationRecord.setVerificationTime(verificationTime);
        verificationRecordMapper.insert(verificationRecord);
//        verificationRecordMapper.insertVerificationRecord(verificationRecord);
//        Long verificationId = verificationRecord.getVerificationId();
//        System.out.println("verificationId: " + verificationId);


        // 4. 创建收支记录
        IncomeExpenseRecord incomeRecord = new IncomeExpenseRecord();
        incomeRecord.setVerificationId(verificationRecord.getVerificationId());
//        incomeRecord.setVerificationId(verificationId);
        incomeRecord.setStoreId(order.getStoreId());
        incomeRecord.setIncreaseAmount(order.getActualAmount());
        incomeExpenseRecordMapper.insert(incomeRecord);

        return verificationTime;
    }


    // 分页查询订单列表
    public TableDataInfo listOrder(Long storeId, Integer orderStatus, String searchKey) {
        // 设置分页参数（注意：必须在查询方法前调用）
        PageUtils.startPage();

        // 执行查询（此时PageHelper会自动拦截并分页）
        List<Map<String, Object>> list = orderMapper.selectOrderList(storeId, orderStatus, searchKey);

        // 获取分页信息（关键修改点）
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);

        // 使用若依框架的返回格式
        return getDataTable(list, pageInfo.getTotal());
    }

    private TableDataInfo getDataTable(List<?> list, long total) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }

    // 查询订单详情
    public Order getDetail(Long orderId) {
        return orderMapper.selectOrderDetail(orderId);
    }


}
